package com.baor.mall.service.impl;

/**
 * @Author: Chen Feiya
 * @CreateTime: 2025-07-18
 * @Description:
 * @Version: 1.0
 */


import com.baor.common.utils.DateUtils;
import com.baor.mall.domain.TBrand;
import com.baor.mall.domain.TCommo;
import com.baor.mall.domain.TLabel;
import com.baor.mall.domain.TTaxonomic;
import com.baor.mall.mapper.TCommoMapper;
import com.baor.mall.mapper.TTaxonomicMapper;
import com.baor.mall.service.ITCommoService;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TCommoServiceImpl implements ITCommoService {
    @Autowired
    private TCommoMapper tCommoMapper;

    @Autowired
    private TTaxonomicMapper tTaxonomicMapper;

    public TCommo selectTCommoById(Long commoId) {
        // 查询得到TCommo对象
        TCommo tCommo = tCommoMapper.selectTCommoById(commoId);
        // 获取返回的字符串
        String labelIdsStr = tCommo.getStrlabelIds();
        // 转换为数组
        String[] labelIds = labelIdsStr != null ? labelIdsStr.split(",") : new String[0];
        tCommo.setLabelIds(labelIds);
        return tCommo;
    }

    public List<TCommo> selectTCommoList(TCommo tCommo) {
        return this.tCommoMapper.selectTCommoList(tCommo);
    }

    public int insertTCommo(TCommo tCommo) {
        tCommo.setCreateTime(DateUtils.getNowDate());
        // 先插入商品，获取商品ID
        int insertCount = this.tCommoMapper.insertTCommo(tCommo); // 插入商品
        Long commoId = tCommo.getCommoId(); // 假设插入后自动设置ID
        // 为确保获取到最新ID
        // 如果没有自动返回ID，建议在Mapper里使用MyBatis选用功能返回自增ID
        String[] labelIds = tCommo.getLabelIds();
//        if (commoId != null && labelIds != null && labelIds.length > 0) {
            this.tCommoMapper.insertLabelIds(commoId, labelIds); // 关联标签
//        }
        return insertCount;
    }

    public int updateTCommo(TCommo tCommo) {
        Long commoId = tCommo.getCommoId();
         this.tCommoMapper.deleteCommoLabels(commoId);
         String[] labelIds = tCommo.getLabelIds();
         System.out.println("labelIds:" + Arrays.toString(labelIds));
         this.tCommoMapper.insertLabelIds(tCommo.getCommoId(), labelIds);
        return this.tCommoMapper.updateTCommo(tCommo);
    }

    public int deleteTCommoByIds(Long[] ids) {
        return this.tCommoMapper.deleteTCommoByIds(ids);
    }

    public int deleteTCommoById(Long commoId) {
        return this.tCommoMapper.deleteTCommoById(commoId);
    }

    @Override
    public List<TLabel> selectTLabelList() {
        return tCommoMapper.selectTLabelList();
    }

    @Override
    public List<TBrand> selectTBrandList() {
        return tCommoMapper.selectTBrandList();
    }


    @Override
    public List<TCommo> selectRecycleBinList(TCommo tCommo) {
        return tCommoMapper.selectRecycleBinList(tCommo);
    }

    @Override
    public int restoreTCommoById(Long commoId) {
        return tCommoMapper.restoreTCommoById(commoId);
    }
    @Override
    public int restoreTCommoByIds(Long[] ids) {
        return tCommoMapper.restoreTCommoByIds(ids);
    }
    @Override
    public int forceDeleteTCommoById(Long commoId) {
        return tCommoMapper.forceDeleteTCommoById(commoId);
    }

    @Override
    public int forceDeleteTCommoByIds(Long[] ids) {
        return tCommoMapper.forceDeleteTCommoByIds(ids);
    }

    // 获取所有可用商品分类（供搜索下拉框）
    @Override
    public List<TTaxonomic> selectAllTaxonomic() {
        TTaxonomic taxonomic = new TTaxonomic();
        taxonomic.setDelFlag(0L);  // 未删除
        taxonomic.setTaxDisplay("0");  // 显示状态
        return tTaxonomicMapper.selectTTaxonomicList(taxonomic);
    }
}
